package com.ktjiaoyu.crm.text;

import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.repository.UserRepository;
import org.junit.Test;

import javax.annotation.Resource;

public class UserRepositoryTester {
    @Resource
    private UserRepository userRepository;

    @Test
    public void testInsert() {//测试新增
        User user = new User("JPA", "123456", 8L, 1);
        userRepository.save(user);
    }

    @Test
    public void testGet() {
        //测试按主键查询
//        User user = userRepository.getOne(2L);//不是使用该方法
        User user = userRepository.findById(2L).get();
        System.out.println("usrName：" + user.getClass());
    }

    @Test

    public void testFindPageByUsrRoleId() {//测试按角色查询用户并分页
        int page = 0, size = 2;//分页要素，页数从0开始
//        //控制分页数据的排序，可以选择升序和降序
//        Sort sort = new Sort(Sort.Direction.DESC, "usrId");
//        //控制分页的辅助类，可以设置页码、每页的数据条数、排序等
//        Pageable pageable = PageRequest.of(page, size, sort);
//        Page<User> userPage = userRepository.findPageByUsrRoleId(8L, pageable);
//        System.out.println("总记录数：" + userPage.getTotalElements());
//        System.out.println("总页数：" + userPage.getTotalPages());
//        System.out.println("当前页数：" + (userPage.getNumber() + 1));
//        System.out.println("每页记录数：" + userPage.getSize());
//        System.out.println("当前页记录数：" + userPage.getNumberOfElements());
//
//        for (User user : userPage.getContent()) {//获得查询记录
//            System.out.println("usrId：" + user.getUsrId());
//        }
//    }

    }
}
